<template>
  <el-card class="box-card">
    <el-row
      slot="header"
      class="clearfix title"
      type="flex"
      justify="space-between"
    >
      <el-col><strong>创建考试</strong></el-col>
      <el-col>
        <el-button
          style="float: right"
          type="primary"
          plain
          size="small"
          @click="goback"
        >返回列表</el-button>
      </el-col>
    </el-row>
    <el-form
      ref="form"
      style="padding-left: 200px"
      label-position="left"
      :rules="formRules"
      :model="form"
      label-width="120px"
    >
      <el-row :gutter="20">
        <el-col :span="12" :offset="0">
          <el-form-item label="考试名称" required prop="exam_name">
            <el-input
              v-model.trim="form.exam_name"
              placeholder="请输入考试名称"
              clearable
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12" :offset="0">
          <el-form-item label="考试类型" prop="exam_type">
            <el-radio-group v-model="form.exam_type" prop="resource">
              <el-radio label="1">阶段测试</el-radio>
              <el-radio label="2">综合考试</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12" :offset="0">
          <el-form-item label="锁屏方式" prop="lock_screen">
            <el-radio-group v-model="form.lock_screen" prop="resource">
              <el-radio label="1">不锁屏</el-radio>
              <el-radio label="2">锁屏</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-form-item label="归属学科" prop="subject_id">
          <el-select
            v-model="form.subject_id"
            placeholder="归属学科"
            clearable
            @change="changes"
          >
            <el-option
              v-for="item in subjectList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="归属目录" prop="directory_id">
          <el-select
            v-model="form.directory_id"
            placeholder="归属目录"
            clearable
            @change="direChange"
          >
            <el-option
              v-for="item in directoryList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="考试时长" prop="exam_time">
          <el-input-number
            v-model="form.exam_time"
            :min="1"
            label="描述文字"
            @change="handleChange"
          />
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="试卷信息" prop="main_paper_id">
          <el-select
            v-model="form.main_paper_id"
            placeholder="选择主考试卷"
            clearable
            @change="$refs.form.validate()"
          >
            <el-option
              v-for="item in paperList"
              :key="item.id"
              :label="item.paper_name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">立即创建</el-button>
          <el-button @click="cancelAdd">取消</el-button>
        </el-form-item>
      </el-row>
    </el-form>
  </el-card>
</template>

<script>
import { mapGetters } from 'vuex'
import { getExamTree, getPaperApi, addExamListApi } from '@/api/exam/exam'
// import { addAdvPosInfoApi, updateAdvPosInfoApi } from "@/api/exam/exam";
export default {
  name: 'AddExam',
  data() {
    return {
      // 弹框控制变量
      dialogShow: false,
      // 表单数据变量
      form: {
        directory_id: '',
        exam_name: '',
        exam_time: '30',
        exam_type: '1',
        lock_screen: '1',
        main_paper_id: '',
        subject_id: ''
      },
      paperList: [], // 表格里的考试信息
      treeList: [],
      // 表单验证规则
      formRules: {
        directory_id: [{ required: true, message: '请输入', trigger: 'blur' }],
        exam_name: [{ required: true, message: '请输入', trigger: 'blur' }],
        exam_time: [{ required: true, message: '请输入', trigger: 'blur' }],
        exam_type: [{ required: true, message: '请输入', trigger: 'blur' }],
        lock_screen: [{ required: true, message: '请输入', trigger: 'blur' }],
        main_paper_id: [{ required: true, message: '请输入', trigger: 'blur' }],
        subject_id: [{ required: true, message: '请输入', trigger: 'blur' }]
      }
    }
  },
  computed: {
    ...mapGetters(['subjectList', 'directoryList'])
  },
  async created() {
    this.getExamTree()
    await this.$store.dispatch('subject/getSubjectList')
  },
  methods: {
    // 获取试卷信息下拉数据
    async changes(id) {
      this.form.directory_id = ''
      this.form.main_paper_id = ''
      await this.$store.commit('subject/setDirectoryList', id)
      await this.$refs.form.validate()
    },
    async direChange() {
      this.form.main_paper_id = ''
      const re = await getPaperApi({
        subject_id: this.form.subject_id,
        directory_id: this.form.directory_id,
        show_state: '1'
      })
      console.log(re)
      this.paperList = re.list
      await this.$refs.form.validate()
    },
    // 获取tree
    async getExamTree() {
      const re = await getExamTree()
      console.log(re)
      this.treeList = re.data.list
    },
    handleChange(value) {
      console.log(value)
    },
    // 返回列表
    goback() {
      this.$emit('back')
    },
    // 提交行为
    async onSubmit() {
      // 表单的校验
      await this.$refs.form.validate()
      await addExamListApi(this.form)
      // 加工处理表单数据(拷贝一份再加工)
      // 请求的发生
      // this.formData.id
      //   ? await updateAdvPosInfoApi(this.formData)
      //   : await addAdvPosInfoApi(this.formData)
      // 提示效果
      this.$message.success('添加成功')
      // 关闭弹框
      this.$emit('back')
      // 列表更新
      this.$emit('updateList')
    },
    // cancelAdd(){

    // },
    // 弹框关闭
    cancelAdd() {
      console.log('取消')
      // 表单重置
      this.form = {
        directory_id: '',
        exam_name: '',
        exam_time: '30',
        exam_type: '1',
        lock_screen: '1',
        main_paper_id: '',
        subject_id: ''
      }
      // 移除校验
      this.$refs.form.clearValidate()
    }
  }
}
</script>

<style lang="scss" scoped></style>
